// 消息
interface ShowToastOptions {
  icon?: 'success' | 'loading' | 'error' | 'none';
  image?: string;
  duration?: number;
  position?: 'top' | 'center' | 'bottom';
  mask?: boolean;
  success?: (result: any) => void;
  fail?: (result: any) => void;
  complete?: (result: any) => void;
}

export const mtToast = (title: string, config: ShowToastOptions = {}) => {
  uni.showToast({
    title: title,
    icon: config.icon || 'success',
    mask: config.mask || true,
    duration: config.duration || 2000,
    success: config.success,
    fail: config.fail,
    complete: config.complete,
  });
};

export const msg = {
  // 成功消息
  success: (title: string, duration = 2000, complete = () => {}) => {
    uni.showToast({
      title: title,
      icon: 'success',
      mask: true,
      duration: duration,
      complete: complete,
    });
  },
  // 错误消息
  error: (title: string, duration = 2000, complete = () => {}) => {
    uni.showToast({
      title: title,
      icon: 'error',
      mask: true,
      duration: duration,
      complete: complete,
    });
  },
  // 文本消息
  text: (title: string, duration = 2000, complete = () => {}) => {
    uni.showToast({
      title: title,
      icon: 'none',
      mask: true,
      duration: duration,
      complete: complete,
    });
  },

  // 加载loading
  loading: (title = '', mask = true) => {
    uni.showLoading({
      title: title,
      mask: mask,
    });
  },

  // 隐藏loading
  hideLoading: () => {
    uni.hideLoading();
  },

  confirm: (
    content: string,
    confirmCallback = () => {},
    cancelCallback = () => {},
    textOption = ['确定', '取消']
  ) => {
    uni.showModal({
      content: content,
      confirmText: textOption[0],
      cancelText: textOption[1],
      success: (res) => {
        if (res.confirm) {
          confirmCallback();
        } else if (res.cancel) {
          cancelCallback();
        }
      },
    });
  },
};
